package com.hyt.it.ogt.kq.main.controller.gov;


import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.entity.ExamArea;
import com.hyt.it.ogt.kq.service.gov.model.param.ExamAreaParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.ExamAreaParamVO;
import com.hyt.it.ogt.kq.service.gov.service.IExamAreaService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskService;
import com.hyt.model.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 考区信息表 前端控制器
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@RestController
@Api(tags = "112.考区管理接口", value = "考区管理接口")
@ApiSort(value = 112)
@RequestMapping("/kw/gov/examArea")
@Slf4j
public class ExamAreaController {

    @Resource
    private IExamAreaService examAreaService;

    @Resource
    private ITaskService iTaskService;

    @ApiOperation(
            value = "12.1 获取考区",
            notes = "30000:获取考区成功; "
    )
    @ApiOperationSort(value = 1)
    @GetMapping("/getExamArea")
    public ApiResponse<Object> getExamArea(PageParam<ExamArea> pageParam, String taskId,String searchText) {

        try {
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(examAreaService.getExamArea(pageParam, taskId, searchText))
                    .build();
        } catch (Exception e) {
            log.error("# 获取考区异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "12.2 查询考点所属区域",
            notes = "30000:查询考点所属区域成功; "
    )
    @ApiOperationSort(value = 2)
    @GetMapping("/getArea")
    public ApiResponse<Object> getArea(String taskId,String examAreaCode) {

        try {
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(examAreaService.getArea(taskId,examAreaCode))
                    .build();
        } catch (Exception e) {
            log.error("# 查询考点所属区域异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_PLACE_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "12.3 查询考生所属区域",
            notes = "30000:查询考生所属区域成功; "
    )
    @ApiOperationSort(value = 3)
    @GetMapping("/getStudentArea")
    public ApiResponse<Object> getStudentArea(String taskId,String examAreaCode) {

        try {
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(examAreaService.getStudentArea(taskId,examAreaCode))
                    .build();
        } catch (Exception e) {
            log.error("# 查询考生所属区域异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_CANDIDATE_FOR_AREA_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "12.4 删除考区",
            notes = "30000:删除考区成功; "
    )
    @ApiOperationSort(value = 4)
    @PostMapping("/removeExamArea")
    public ApiResponse<Object> removeExamArea(@RequestBody ExamAreaParam examAreaParam) {

        try {
            // 校验任务数据锁定状态(后续版本考虑优化为从缓存中获取状态)
            iTaskService.isLockDataThrowException(examAreaParam.getTaskId());
            examAreaService.removeExamArea(examAreaParam.getTaskId(),examAreaParam.getExamAreaCode());
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("删除考区业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 删除考区异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_REMOVE_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "12.5 添加和修改考区",
            notes = "30000:添加和修改考区成功; "
    )
    @ApiOperationSort(value = 5)
    @PostMapping("/addOrUpdateExamArea")
    public ApiResponse<Object> addOrUpdateExamArea(@Valid @RequestBody ExamAreaParamVO examAreaParamVO) {
        try {
            // 校验任务数据锁定状态(后续版本考虑优化为从缓存中获取状态)
            iTaskService.isLockDataThrowException(examAreaParamVO.getTaskId());
            if (StringUtils.isBlank(examAreaParamVO.getExamAreaName()) ||
                    StringUtils.isBlank(examAreaParamVO.getAreaCodes()) ||
                            StringUtils.isBlank(examAreaParamVO.getStudentAreaCodes())) {

                return ApiResponse.builder()
                        .code(ResponseCode.EXAM_AREA_REQUEST_PARAM_NAME_AREA_CAN_NOT_NULL.getCode())
                        .build();
            }

            examAreaService.addOrUpdateExamArea(examAreaParamVO);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("添加和修改考区业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 添加和修改考区异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_ADD_UPDATE_EXCEPTION.getCode())
                    .build();
        }
    }

}
